<?php
//$type:
define('GL_USE_ID_1',1); //parent when expand
define('GL_ID_1_IS_GROUP',2); //should use id 1, output type
define('GL_USE_ID_2',4); //subject
define('GL_ID_2_IS_GROUP',8); //should use id 2
define('GL_OUTPUT_IS_CHILD',0x10);
define('GL_USE_DIRECT_LINK',0x40); //group link
define('GL_USE_DIRECT_UNLINK',0x80); //unlink to parent(id1)
define('GL_USE_REL_UNLINK',0x100); //unlink to id2
define('GL_USE_CHECK',0x200); //if id1-id2 link exist, output 'checked'
//combined:
define('GL_INHERIT',GL_ID_1_IS_GROUP|GL_USE_ID_2|GL_ID_2_IS_GROUP|GL_USE_DIRECT_LINK|GL_USE_DIRECT_UNLINK|GL_USE_REL_UNLINK|GL_USE_CHECK);
define('GL_EXPAND_BASE',GL_USE_ID_1|GL_OUTPUT_IS_CHILD);
define('GL_USE_COLUM_2',GL_USE_DIRECT_LINK|GL_USE_DIRECT_UNLINK|GL_USE_REL_UNLINK);
//specialized:
define('GL_GROUP_HOMEPAGE',		GL_USE_ID_1|GL_ID_1_IS_GROUP|GL_OUTPUT_IS_CHILD|GL_USE_DIRECT_LINK|GL_USE_DIRECT_UNLINK);
define('GL_GROUP_NODE_LIST',	GL_USE_ID_2|GL_ID_2_IS_GROUP|GL_OUTPUT_IS_CHILD|GL_USE_REL_UNLINK);
define('GL_GROUP_PARENT_LIST',	GL_ID_1_IS_GROUP|GL_USE_ID_2|GL_ID_2_IS_GROUP);
define('GL_GROUP_CHILD_LIST',	GL_ID_1_IS_GROUP|GL_USE_ID_2|GL_ID_2_IS_GROUP|GL_OUTPUT_IS_CHILD|GL_USE_REL_UNLINK);
define('GL_GROUP_LINK_GROUP',	GL_USE_ID_1|GL_ID_1_IS_GROUP|GL_USE_ID_2|GL_ID_2_IS_GROUP|GL_OUTPUT_IS_CHILD|GL_USE_CHECK);
define('GL_NODE_GROUP_LIST',	GL_ID_1_IS_GROUP|GL_USE_ID_2|GL_USE_REL_UNLINK);
define('GL_NODE_LINK_GROUP',	GL_USE_ID_1|GL_ID_1_IS_GROUP|GL_USE_ID_2|GL_OUTPUT_IS_CHILD|GL_USE_CHECK);

function get_group_list($type,$id1=0,$id2=0) {
	$list=array();
	$query='';
	$type=(int)$type;
	if($type & GL_ID_2_IS_GROUP)$id2_type='group'; else $id2_type='node';
	if($type & GL_ID_1_IS_GROUP) { //输出group
		$query = 'select distinct id, title, description, count, parent_count from `group` ';
		if($type & GL_USE_ID_1) { //从头开始或者group节点展开
			if($id1>0) {
				if($type & GL_OUTPUT_IS_CHILD) {
					$query .= "inner join link on link.component = group.id and link.type = 'group' and link.container = $id1 ";
				}//else：不可能
			} else {
				$query .= 'where parent_count=0 ';
			}
		} else { //第一次列表，用id2
			if($id2<=0) return $list; //bad request
			if($type & GL_OUTPUT_IS_CHILD) { //group输出子group
				$query .= "inner join link on link.component = group.id and link.type = 'group' and link.container = $id2 ";
			} else {
				$query .= 'inner join link on link.container = group.id ';
				//group|node 输出父group
				$query .= "and link.type = '$id2_type' and link.component = $id2 ";
			}
		}
	} else { //输出node
		$query = 'select id, title, child_count as count, 0 as parent_count from node ';
		if($type & GL_USE_ID_1) { //node展开TODO
			if($id1>0) {
				$query .= "where parent=$id1 ";
			} else {
				$query .= 'where parent=-1 ';
			}
		} else { //第一次列表，用id2
			//if($type & GL_OUTPUT_IS_CHILD) //必然的
			if($type & GL_ID_2_IS_GROUP) { //group输出子node
				$query .= "inner join link on link.component = node.id and link.type = 'node' and link.container = $id2 ";
			}// else：node输出子node，TODO
		}
	}
	$query .= 'order by title asc ';
	
	$ret=oj_query($query);
	$i=0;
	while($row=mysql_fetch_assoc($ret)) {
		$list[$i] = array(
			0	=> htmlspecialchars($row['title']), //'title'
			1	=> $row['id'], //'id'
			2	=> $row['count'], //'count'
			3	=> $row['parent_count'] //'parent_count'
		);
		//节点连组的checkbox
		if($type & GL_USE_CHECK) {
			$row_check=mysql_fetch_row(oj_query(
				"select create_time from link where container={$row['id']} and component=$id2 and type='$id2_type'"
			));
			if(isset($row_check[0]))$list[$i][4]='checked';
		}
		$i++;
	}
	return $list;
}

function draw_group_list($type,$id1,$id2,$exe=1) {
	include_once 'form.php';
	$table_head=array();
	if($type & GL_ID_1_IS_GROUP) {
		$table_head[0]=_d('g_title');
	} else {
		$table_head[0]=_d('n_title');
	}
	if($type & GL_USE_COLUM_2) {
		$table_head[1]=_d('g_action');
		$table_colspan='colspan=2';
	}
	if($type & GL_USE_CHECK) {
		draw_simple_form_head('post','group.php?action=links&do=1');
	}
	draw_tablelist_head($table_head);
	echo "<tr><td $table_colspan id='cell_group_list_$type'>";
	if($type==GL_GROUP_HOMEPAGE||$type==GL_GROUP_CHILD_LIST) {
		echo '<a href="group.php?action=add">' . _d('g_add') . '</a>';
	} else if($type==GL_NODE_GROUP_LIST||$type==GL_GROUP_PARENT_LIST) {
		echo "<a href='group.php?action=link&id2=$id2&type=" . ($type&GL_ID_2_IS_GROUP?'group':'node') . '\'>' . _d('link_ng') . '</a>';
	}
	echo '</td></tr></table>';
	if($type & GL_USE_CHECK) {
		draw_simple_form_fillin(array('submit'=>array('submit','',_d('g_save'))));
		echo "<input type=hidden name='id2' value='$id2' id='group_link_child'>";
		echo '<input type=hidden name="type" value="' . ($type&GL_ID_2_IS_GROUP?'group':'node') . '">';
		draw_simple_form_foot();
	}
	if($type & GL_USE_DIRECT_LINK) {
		global $group_ajax; //TODO
		draw_simple_form('get',"group.php",
			array(
				'action'=>'link',
				'ajax'=>$group_ajax,
				'type'=>'group'),
			array(
				'id1'=>array('input',_d('g_link_parent'),'','id="group_link_parent"'),
				'id2'=>array('input',_d('g_link_child'),'','id="group_link_child"'),
				'submit'=>array('submit','',_d('g_link'))
			));
	}
	$ret_script="drawGroupList($(\"cell_group_list_$type\"),$id1,$id2,$type);";
	if($exe)echo "<script>$ret_script</script>";
	else return $ret_script;
}

?>
